﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SimpleHttpServer
{
    public class CommonHelper
    {
        public static string AppPath
        {
            get
            {
                string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

                string[] spl = path.Split(new string[] {"file:\\"}, StringSplitOptions.RemoveEmptyEntries);

                return spl[0];
            }
        }

        public static Dictionary<string, string> CreatePostDictionary(string postData)
        {
            Dictionary<string, string > dict = new Dictionary<string, string>();

            string[] pairs = postData.Split(new[] {"&"}, StringSplitOptions.RemoveEmptyEntries);

            foreach (var pair in pairs)
            {
                string[] values = pair.Split(new[] {"="}, StringSplitOptions.RemoveEmptyEntries);
                if (values.Length != 2) continue;

                string key = values[0];
                string value = values[1];

                if (!dict.ContainsKey(key))
                    dict.Add(key, value);
                    
            }

            return dict;
        }

       
    }
}
